package com.slz.crm.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slz.crm.common.annotation.RequirePermission;
import com.slz.crm.common.enumeration.PermissionOperates;
import com.slz.crm.common.result.Result;
import com.slz.crm.common.untils.BaseUnit;
import com.slz.crm.pojo.ao.RoleAO;
import com.slz.crm.pojo.dto.UserDTO;
import com.slz.crm.pojo.vo.UserVO;
import com.slz.crm.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param userDTO
     * @return jwt token
     */
    @PostMapping("/login")
    private Result<String> login(@RequestBody UserDTO userDTO) {
        String token = userService.login(userDTO);
        return Result.success(token);
    }

    /**
     * 添加系统用户
     *
     * @param userDTO
     * @return 初始密码
     */
    @PostMapping("/add")
    @RequirePermission(
            permClass = PermissionOperates.SystemOperate.class,
            permName = "CREATE_USER"
    )
    private Result<String> addUser(@RequestBody UserDTO userDTO) {
        String password = userService.addUser(userDTO);
        return Result.success(password);
    }

    /**
     * 查询所有用户
     *
     * @return 所有用户
     */
    @GetMapping
    @RequirePermission(
            permClass = PermissionOperates.SystemOperate.class,
            permName = "VIEW_USER"
    )
    private Result<Page<UserVO>> getAllUser(Integer pageNum, Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            pageNum = 1;
            pageSize = 10;
        }
        Page<UserVO> userDTOS = userService.getAllUser(pageNum, pageSize);
        return Result.success(userDTOS);
    }

    /**
     * 更新密码
     *
     * @param password 新密码
     * @return
     */
    @PostMapping("/password")
    @RequirePermission(
            permClass = PermissionOperates.SystemOperate.class,
            permName = "UPDATE_PASSWORD"
    )
    private Result<Boolean> updatePassword(@RequestBody String password) {

        boolean b = userService.updatePassword(password);

        return Result.success();
    }

    /**
     * 更新用户信息(无法修改密码)
     *
     * @param userDTO 用户信息
     * @return
     */
    @PostMapping("/update")
    @RequirePermission(
            permClass = PermissionOperates.SystemOperate.class,
            permName = "UPDATE_USER_INFO"
    )
    private Result<String> updateUser(@RequestBody UserDTO userDTO) {
        userService.updateUser(userDTO);
        return Result.success();
    }

    @GetMapping("/my")
    public Result<UserVO> getMyUser() {
        RoleAO currentRole = BaseUnit.getCurrentRole();
        Long id = currentRole.getId();
        UserVO byId = userService.getById(id);
        return Result.success(byId);
    }

}
